<template>
  <div class="menu">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-button @click="goToMenuCreate">添加菜单</el-button>
      </div>
      <el-table :data="menus" style="width: 100%">
        <el-table-column type="index"  label="编号" min-width="150">
        </el-table-column>
        <el-table-column prop="name" label="菜单名称" min-width="150">
        </el-table-column>
        <el-table-column prop="level" label="菜单级数" min-width="150">
        </el-table-column>
        <el-table-column prop="icon" label="前端图标" min-width="150">
        </el-table-column>
        <el-table-column prop="orderNum" label="排序" min-width="150">
        </el-table-column>
        <el-table-column label="操作" min-width="150">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
              >编辑</el-button
            >
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script lang="ts">
// createdBy: "system"
// createdTime: "2020-10-27T23:11:51.000+0800"
// description: "管理系统角色、菜单、资源"
// href: ""
// icon: "lock"
// id: 1
// level: 0
// name: "权限管理"
// orderNum: 1
// parentId: -1
// shown: true
// updatedBy: "18201288771"
// updatedTime: "2
import Vue from 'vue'
import { getAll, deleteMenu } from './api/index'
interface Row {
  id: number;
}
export default Vue.extend({
  name: 'MenuIndex',
  methods: {
    goToMenuCreate () {
      this.$router.push({
        name: 'menu-create'
      })
    },
    async getAll () {
      const { data } = await getAll()
      console.log(data)
      if (data.code === '000000') {
        this.menus = data.data
      }
    },
    handleEdit (index: number, row: Row) {
      console.log(index, row)
      this.$router.push({
        name: 'menu-edit',
        params: {
          id: row.id
        }
      })
    },
    async handleDelete (index: number, row: Row) {
      console.log(index, row)
      let res = null
      try {
        res = await this.$confirm('确认删除吗?', '删除提示', {})
        console.log(res)
        await this.deleteMenu(row.id)
      } catch (error) {
        console.log(error)
        this.$message('已取消删除')
      }
    },
    async deleteMenu (id: number) {
      const res = await deleteMenu(id)
      console.log(res)
      if (res.data.code === '000000') {
        this.$message.success('删除成功')
        await this.getAll()
      }
    }
  },
  async created () {
    await this.getAll()
  },
  data () {
    return {
      menus: []
    }
  }
})
</script>

<style lang="scss" scoped></style>
